package com.dlyk.controller;

import com.dlyk.bean.TActivity;
import com.dlyk.query.ActivityQuery;
import com.dlyk.result.R;
import com.dlyk.service.ActivityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 活动分页查询
     * @param current
     * @return
     */
    @GetMapping("/api/activities")
    public R activityPage(@RequestParam Integer current,ActivityQuery activityQuery){
        if (current==null){
            current=1;
        }
        PageInfo<TActivity> pageInfo = activityService.getActivityByPage(current,activityQuery);
        return R.OK(pageInfo);
    }

    /**
     * 录入市场活动
     * @param activityQuery
     * @param token
     * @return
     */
    @PostMapping("/api/activity")
    public R addActivity(ActivityQuery activityQuery, @RequestHeader("Authorization") String token){
        activityQuery.setToken(token);
        int activity=activityService.saveActivity(activityQuery);
        return activity>0 ? R.OK(activity): R.FAIL();
    }

    /**
     * 根据id查询市场活动信息
     * @param id
     * @return
     */
    @GetMapping("/api/activity/{id}")
    public R loadActivity(@PathVariable Integer id){
        TActivity activity=activityService.selectById(id);
        return R.OK(activity);
    }

    /**
     * 编辑市场活动
     * @param activityQuery
     * @param token
     * @return
     */
    @PutMapping("/api/activity")
    public R editActivity(ActivityQuery activityQuery, @RequestHeader("Authorization") String token){
        activityQuery.setToken(token);
        int updated=activityService.updateActivity(activityQuery);
        return updated>0 ? R.OK(): R.FAIL();
    }
}
